package com.DAO.Impl;

import com.DAO.ScheduleDAO;
import com.DBUtil.DBUtil;
import com.entity.Schedule;
import com.entity.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 20x
 * @date 2024 - 12 -13
 **/
public class ScheduleDAOImpl implements ScheduleDAO {

    @Override
    public Schedule querySchedule(String studentId,String ClassIndex) {
        String ClassIndex1 = null;
        if(ClassIndex.equals("第一节课")){
            ClassIndex1 = "firstClass";
        }else if(ClassIndex.equals("第二节课")){
            ClassIndex1 = "secondClass";
        }else if(ClassIndex.equals("第三节课")){
            ClassIndex1 = "thirdClass";
        } else if (ClassIndex.equals("第四节课")) {
            ClassIndex1 = "fourthClass";
        }
        String sql = "SELECT ClassType,time,"+ClassIndex1+",classTime,ClassStatus from CourseSchedule s left join classAll c on s.classId=c.readyId where stuId=? and ClassTime = ?";

        ResultSet rs = null;
        PreparedStatement pst = null;
        Connection conn = DBUtil.getConnection();
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, studentId);
            pst.setString(2, ClassIndex);
            System.out.println(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                Schedule schedule = new Schedule(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5));
                return schedule;
            }
            DBUtil.close(rs,pst,conn);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<String> queryScheduleIndex(String studentId) {
        //根据id学生上课记录
        List<String> list = new ArrayList<>();
        String sql = "select classTime from CourseSchedule where stuId=?";

        ResultSet rs = null;
        PreparedStatement pst = null;
        Connection conn = DBUtil.getConnection();
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, studentId);
            rs = pst.executeQuery();
            while (rs.next()) {
                list.add(rs.getString(1));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Student> queryStudentByStuId(String classIndex,String time) {
        List<Student> Lists = new ArrayList<>();
        String sql="SELECT s.stuId,s.stuName FROM CourseSchedule c LEFT JOIN student s ON s.stuId = c.stuId WHERE c.classId = ? and ";
        if(time.equals("AM 8.00~9.00")){
            sql+="c.ClassTime = '第一节课'";
        }else if (time.equals("AM 9.30~10.30")){
            sql+="c.ClassTime = '第二节课'";
        }else if (time.equals("PM 14.00~15.00")){
            sql+="c.ClassTime = '第二节课'";
        }else if (time.equals("PM 15.30~16.30")){
            sql+="c.ClassTime = '第四节课'";
        }
        ResultSet rs = null;
        PreparedStatement pst = null;
        Connection conn = DBUtil.getConnection();
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, classIndex);
            rs = pst.executeQuery();
            while (rs.next()) {
                Student student = new Student();
                student.setStuId(rs.getString(1));
                student.setStuName(rs.getString(2));
                Lists.add(student);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return Lists;
    }

    @Override
    public int dropCourse(String classIndex,String time) {
        int count = 0;
        String sql = "delete from CourseSchedule where classId = ? and ClassTime = ?";
        if(time.equals("AM 8.00~9.00")){
            time = "第一节课";
        } else if (time.equals("AM 9.30~10.30")){
            time = "第二节课";
        } else if (time.equals("PM 14.00~15.00")){
            time = "第三节课";
        } else if (time.equals("PM 15.30~16.30")){
            time= "第四节课";
        }
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, classIndex);
            pst.setString(2,time);
            count = pst.executeUpdate();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int addCourseed(String classIndex, String studentId, String time, String status) {
        int count = 0;
        String sql = "insert into CourseSchedule values(?,?,?,?)";
        if(time.equals("AM 8.00~9.00")){
            time = "第一节课";
        } else if (time.equals("AM 9.30~10.30")){
            time = "第二节课";
        } else if (time.equals("PM 14.00~15.00")){
            time = "第三节课";
        } else if (time.equals("PM 15.30~16.30")){
            time= "第四节课";
        }
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, studentId);
            pst.setString(2, classIndex);
            pst.setString(3, status);
            pst.setString(4, time);
            count = pst.executeUpdate();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    public List<Schedule> queryScheduleAll(String studentId) {
        List<Schedule> list = new ArrayList<>();
        List<String> scheduleIndex = queryScheduleIndex(studentId);
        System.out.println(scheduleIndex);
        for(int i=0;i<scheduleIndex.size();i++){
            System.out.println(scheduleIndex.get(i));
            Schedule schedule = querySchedule(studentId,scheduleIndex.get(i));
            list.add(schedule);
        }
        return list;
    }
    public static void main(String[] args) {
        ScheduleDAOImpl scheduleDAO = new ScheduleDAOImpl();
//        System.out.println(scheduleDAO.querySchedule("S001","第一节课"));
//        System.out.println(scheduleDAO.queryScheduleIndex("S001"));
//        System.out.println(scheduleDAO.queryScheduleAll("S001"));
        System.out.println(scheduleDAO.queryStudentByStuId("2","AM 8.00~9.00"));
    }

}
